/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.common.core.constant.enums.*;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.order.api.dto.OrderCommentDTO;
import com.pig4cloud.pigx.order.api.entity.OrderComment;
import com.pig4cloud.pigx.order.api.entity.OrderCommentDetail;
import com.pig4cloud.pigx.order.api.entity.OrderInfo;
import com.pig4cloud.pigx.order.api.vo.OrderCommentVO;
import com.pig4cloud.pigx.order.mapper.OrderCommentMapper;
import com.pig4cloud.pigx.order.service.OrderCommentDetailService;
import com.pig4cloud.pigx.order.service.OrderCommentService;
import com.pig4cloud.pigx.order.service.OrderInfoService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单评论
 *
 * @author pigx code generator
 * @date 2023-11-29 14:51:38
 */
@Service
@AllArgsConstructor
public class OrderCommentServiceImpl extends ServiceImpl<OrderCommentMapper, OrderComment>
		implements OrderCommentService {

	private final OrderCommentDetailService orderCommentDetailService;

	private final OrderInfoService orderInfoService;

	/**
	 * 分页获取评论信息
	 * @param page
	 * @param orderCommentDTO
	 * @return
	 */
	@Override
	public IPage<OrderCommentVO> pageList(Page page, OrderCommentDTO orderCommentDTO) {
		IPage<OrderCommentVO> result = new Page<>();
		LambdaQueryWrapper<OrderComment> lqwOrderComment = new LambdaQueryWrapper<>();
		if (orderCommentDTO.getSpuId() != null) {
			lqwOrderComment.eq(OrderComment::getSpuId, orderCommentDTO.getSpuId());
		}
		lqwOrderComment.orderByDesc(OrderComment::getCreateTime);
		lqwOrderComment.eq(OrderComment::getDelFlag, DeleteFlagEnum.VALID.getType());
		IPage<OrderComment> items = this.page(page, lqwOrderComment);
		if (items.getRecords().size() > 0) {
			result = items.convert(u -> {
				OrderCommentVO v = new OrderCommentVO();
				BeanUtils.copyProperties(u, v);

				// 获取评论ID号
				Long orderCommentId = v.getId();

				// 获取第一次用户评价
				LambdaQueryWrapper<OrderCommentDetail> lqwOrderCommentDetail = new LambdaQueryWrapper<>();
				lqwOrderCommentDetail.eq(OrderCommentDetail::getHasFirst, BooleanTypeEnum.TRUE.getType());
				lqwOrderCommentDetail.eq(OrderCommentDetail::getOrderCommentId, orderCommentId);
				lqwOrderCommentDetail.eq(OrderCommentDetail::getCommentType, CommentTypeEnum.USER.getType());
				OrderCommentDetail userComment = orderCommentDetailService.getOne(lqwOrderCommentDetail);
				v.setUserFirstComment(userComment);

				// 获取第一次平台回复
				lqwOrderCommentDetail = new LambdaQueryWrapper<>();
				lqwOrderCommentDetail.eq(OrderCommentDetail::getHasFirst, BooleanTypeEnum.TRUE.getType());
				lqwOrderCommentDetail.eq(OrderCommentDetail::getOrderCommentId, orderCommentId);
				lqwOrderCommentDetail.eq(OrderCommentDetail::getCommentType, CommentTypeEnum.PLATFORM.getType());
				OrderCommentDetail platformComment = orderCommentDetailService.getOne(lqwOrderCommentDetail);
				v.setPlatformFirstComment(platformComment);
				return v;
			});
		}
		return result;
	}

	/**
	 * 买家评论
	 * @param orderCommentVO
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public R buyerComment(OrderCommentVO orderCommentVO) {
		OrderComment orderComment = new OrderComment();
		BeanUtils.copyProperties(orderCommentVO, orderComment);
		if (orderComment.getShopUserId() == null) {
			// 没有用户ID的场合，属于平台导入
			orderComment.setCommentSource(CommentSourceEnum.PLATFORM.getType());
			// 随机生成shopUserId
			// DefaultIdentifierGenerator defaultIdentifierGenerator = new
			// DefaultIdentifierGenerator();
			// Long tempShopUserId = defaultIdentifierGenerator.nextId(null);
			// orderComment.setShopUserId(tempShopUserId);
		}
		else {
			// 存在ID的场合，属于买家
			orderComment.setCommentSource(CommentSourceEnum.USER.getType());
		}

		this.save(orderComment);

		OrderCommentDetail detail = new OrderCommentDetail();
		detail.setOrderCommentId(orderComment.getId());
		detail.setCommentType(CommentTypeEnum.USER.getType());
		detail.setHasFirst(BooleanTypeEnum.TRUE.getType());
		detail.setContent(orderCommentVO.getUserFirstComment().getContent());
		detail.setCommentImages(orderCommentVO.getUserFirstComment().getCommentImages());
		orderCommentDetailService.save(detail);

		// 存在订单id的场合,修改订单状态为已评论
		if (orderCommentVO.getOrderId() != null) {
			OrderInfo orderInfo = new OrderInfo();
			orderInfo.setId(orderCommentVO.getOrderId());
			orderInfo.setOrderStatus(OrderStatusEnum.COMMENT.getType());
			orderInfo.setOrderCommentId(orderComment.getId());
			orderInfoService.updateById(orderInfo);
		}

		return R.ok();
	}

	/**
	 * 平台回复
	 * @param orderCommentDetail
	 * @return
	 */
	@Override
	public R platformReply(OrderCommentDetail orderCommentDetail) {
		// 查看评论是否存在
		OrderComment orderComment = this.getById(orderCommentDetail.getOrderCommentId());
		if (orderComment == null) {
			return R.failed("评论不存在");
		}
		orderCommentDetail.setCommentType(CommentTypeEnum.PLATFORM.getType());

		// 判断首次评论是否存在
		LambdaQueryWrapper<OrderCommentDetail> lqwOrderCommentDetail = new LambdaQueryWrapper<>();
		lqwOrderCommentDetail.eq(OrderCommentDetail::getOrderCommentId, orderCommentDetail.getOrderCommentId());
		lqwOrderCommentDetail.eq(OrderCommentDetail::getHasFirst, BooleanTypeEnum.TRUE.getType());
		lqwOrderCommentDetail.eq(OrderCommentDetail::getCommentType, CommentTypeEnum.PLATFORM.getType());
		lqwOrderCommentDetail.eq(OrderCommentDetail::getDelFlag, DeleteFlagEnum.VALID.getType());
		OrderCommentDetail platformFirstComment = orderCommentDetailService.getOne(lqwOrderCommentDetail);
		if (platformFirstComment == null) {
			// 不存在第一次评论的场合
			orderCommentDetail.setHasFirst(BooleanTypeEnum.TRUE.getType());
		}
		else {
			// 存在第一次评论的场合
			orderCommentDetail.setHasFirst(BooleanTypeEnum.FALSE.getType());
		}

		return R.ok(orderCommentDetailService.save(orderCommentDetail));
	}

	/**
	 * 修改平台回复
	 * @param orderCommentDetail
	 * @return
	 */
	@Override
	public R updatePlatformReply(OrderCommentDetail orderCommentDetail) {

		LambdaQueryWrapper<OrderCommentDetail> lqwOrderCommentDetail = new LambdaQueryWrapper<>();
		lqwOrderCommentDetail.eq(OrderCommentDetail::getOrderCommentId, orderCommentDetail.getOrderCommentId());
		lqwOrderCommentDetail.eq(OrderCommentDetail::getHasFirst, BooleanTypeEnum.TRUE.getType());
		lqwOrderCommentDetail.eq(OrderCommentDetail::getCommentType, CommentTypeEnum.PLATFORM.getType());
		lqwOrderCommentDetail.eq(OrderCommentDetail::getDelFlag, DeleteFlagEnum.VALID.getType());
		OrderCommentDetail platformFirstComment = orderCommentDetailService.getOne(lqwOrderCommentDetail);
		platformFirstComment.setContent(orderCommentDetail.getContent());

		return R.ok(orderCommentDetailService.updateById(platformFirstComment));
	}

	/**
	 * 获取评论详情
	 * @param id
	 * @return
	 */
	@Override
	public OrderCommentVO getOrderCommentById(Long id) {
		OrderCommentVO result = new OrderCommentVO();
		OrderComment orderComment = this.getById(id);
		BeanUtils.copyProperties(orderComment, result);

		// 获取用户首次评论
		LambdaQueryWrapper<OrderCommentDetail> lqwOrderCommentDetail = new LambdaQueryWrapper<>();
		lqwOrderCommentDetail.eq(OrderCommentDetail::getCommentType, CommentTypeEnum.USER.getType());
		lqwOrderCommentDetail.eq(OrderCommentDetail::getHasFirst, BooleanTypeEnum.TRUE.getType());
		lqwOrderCommentDetail.eq(OrderCommentDetail::getOrderCommentId, id);
		OrderCommentDetail userFirstComment = orderCommentDetailService.getOne(lqwOrderCommentDetail);
		result.setUserFirstComment(userFirstComment);
		return result;
	}

	/**
	 * 修改用户首批评论
	 * @param orderCommentVO
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateBuyerComment(OrderCommentVO orderCommentVO) {
		OrderComment orderComment = new OrderComment();
		BeanUtils.copyProperties(orderCommentVO, orderComment);
		this.updateById(orderComment);
		orderCommentDetailService.updateById(orderCommentVO.getUserFirstComment());
		return true;
	}

}
